﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using SocialIn.Core.Data;
using System.Data;
using System.Web;
using System.Web.Security;

namespace SocialIn.Biz
{
    public sealed class Connect
    {
        ConnectionStringSettings _BaseDBCon = null;

        public Connect()
        {
            _BaseDBCon = ConfigurationManager.ConnectionStrings[ConfigName.DBConfig];
        }

        public bool LogIn(string userID, string plainPassword, out Result<bool> resultValue)
        {
            resultValue = new Result<bool>();

            string hashPwd = Convert.ToBase64String(Encoding.UTF8.GetBytes(plainPassword));

            string query = string.Format(@" SELECT A.UserID, A.UserName, A.Password, A.Email, A.SelfIntroduction, A.PicturePath, A.JoinVerified
	                                            , B.USERTYPE, B.USERTYPENAME
                                            FROM 
                                            (
	                                            SELECT 
		                                            A.UserID, A.UserName, A.Password, A.Email, A.SelfIntroduction, A.PicturePath, A.JoinVerified
		                                            , B.RoleID
	                                            FROM Member AS A 
	                                            INNER JOIN 
	                                            MembersInRoles AS B 
	                                            ON A.UserID = B.UserID 
	                                            WHERE A.Seceded = 0 AND A.JoinVerified = 1 AND A.USERID = '{0}' AND PASSWORD = '{1}'
                                            ) AS A 
                                            INNER JOIN 
                                            (
	                                            SELECT SubCategory AS USERTYPE, CodeName AS USERTYPENAME 
	                                            FROM CodeMaster 
	                                            WHERE CATEGORY = 'M002'
                                            ) AS B 
                                            ON A.RoleID = B.USERTYPE ", userID, plainPassword);
            Dictionary<string, string> parameters = new Dictionary<string,string>();


            SqlHelper sql = new SqlHelper(_BaseDBCon.ProviderName, _BaseDBCon.ConnectionString);
            Result<DataTable> dtResult = sql.ExecuteDataTable(query, parameters);

            if (dtResult.ReturnValue == null || dtResult.ReturnValue.Rows.Count == 0)
            {
                resultValue.Completed = false;
                resultValue.Message = "아이디와 비밀번호를 확인하세요.";
                resultValue.Error = dtResult.Error;
                return false;
            }
            else
            {
                Member currentUser = new Member();
                currentUser.UserID = dtResult.ReturnValue.Rows[0]["UserID"].ToString();
                currentUser.UserName = dtResult.ReturnValue.Rows[0]["UserName"].ToString();
                currentUser.UserType = dtResult.ReturnValue.Rows[0]["USERTYPE"].ToString();
                currentUser.UserTypeName = dtResult.ReturnValue.Rows[0]["USERTYPENAME"].ToString();
                currentUser.PicturePath = dtResult.ReturnValue.Rows[0]["PicturePath"].ToString();

                HttpContext.Current.Session[ConfigName.SessionMember] = currentUser;
                //FormsAuthentication.RedirectFromLoginPage(userID, true);
                return true;
            }
            return false;
        }

        public void LogOut(string rediretPath)
        {
            HttpContext.Current.Session[ConfigName.SessionMember] = null;
            FormsAuthentication.SignOut();

            HttpContext.Current.Response.Redirect(rediretPath);
        }
    }
}
